package com.bonjour.common.initializ;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.bonjour.common.helper.SpringHelper;

public abstract class SpringBootApplication implements CommandLineRunner {
    private final String APP;
    protected final Logger LOG;

    public SpringBootApplication() {
        Class<?> clz = this.getClass();
        this.APP = clz.getSimpleName();
        this.LOG = LoggerFactory.getLogger(clz);
    }

    @Override
    public final void run(String... args) {
    	System.out.println("***************");
        LOG.info("{} on at {} environment", APP, SpringHelper.applicationEnv());
        // Logback Reset Initializer
        new LogbackResetInitializer().configure();
        LOG.debug("COUNT={} jobs run", jobs(args));
    }

    /** spring boot 服务启动 **/
    protected static ConfigurableApplicationContext start(Class<?> mainCLZ, String... args) {
        try {
            return SpringApplication.run(mainCLZ, args);
        } catch (Exception e) {
            LoggerFactory.getLogger(mainCLZ).info("{} startup failure then terminated ", mainCLZ.getSimpleName(), e);
            System.exit(Integer.MAX_VALUE); return null;
        }
    }
    /** 后台任务入口 **/
    protected abstract int jobs(String... args);
}
